﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace QuanLyGaRaOTo.PhieuTiepNhan
{
    public class DAO
    {
        private string cnStr = new DataProvider().getcnStr();
        private SqlConnection cn;
        private string procName;
        private SqlCommand cmd;
        private DataTable Kq;
        private SqlDataAdapter da;

        public DAO()
        {
            try
            {
                cn = new SqlConnection(cnStr);
            }
            catch (Exception e)
            {
                throw new Exception("Không kết nối được cơ sở dữ liệu!!! " + e.Message);
            }
        }

        public DataTable read()
        {
            cn.Open();
            Kq = new DataTable();
            procName = "stpXemPhieuTiepNhan";
            try
            {
                da = new SqlDataAdapter(procName, cn);
                da.Fill(Kq);
            }
            catch { }
            cn.Close();
            return Kq;
        }

        public int insert(DTO dto)
        {
            cn.Open();
            procName = "stpThemPhieuTiepNhan";
            try
            {
                cmd = new SqlCommand(procName, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@TenChuXe", SqlDbType.NVarChar).Value = dto.TenChuXe;
                cmd.Parameters.Add("@BienSo", SqlDbType.VarChar).Value = dto.BienSo;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = dto.DiaChi;
                cmd.Parameters.Add("@DienThoai", SqlDbType.VarChar).Value = dto.DienThoai;
                cmd.Parameters.Add("@HieuXe", SqlDbType.Int).Value = dto.HieuXe;
                cmd.Parameters.Add("@NgayTiepNhan", SqlDbType.DateTime).Value = dto.NgayTiepNhan;
                cmd.Parameters.Add("@MaPhieu", SqlDbType.Int).Direction = ParameterDirection.Output;

                cmd.ExecuteNonQuery();
            }
            catch
            {
                return -1;
            }
            finally
            {
                cn.Close();
            }
            return Convert.ToInt32(cmd.Parameters["@MaPhieu"].Value);
        }

        public Boolean delete(DTO dto)
        {
            cn.Open();
            procName = "stpXoaPhieuTiepNhan";
            try
            {
                cmd = new SqlCommand(procName, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@MaPhieu", SqlDbType.Int).Value = dto.MaPhieu;
                cmd.ExecuteNonQuery();
            }
            catch
            {
                return false;
            }
            finally
            {
                cn.Close();
            }
            return true;
        }

        public Boolean update(DTO dto)
        {
            cn.Open();
            procName = "stpCapNhatPhieuTiepNhan";
            try
            {
                cmd = new SqlCommand(procName, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@MaPhieu", SqlDbType.Int).Value = dto.MaPhieu;
                cmd.Parameters.Add("@TenChuXe", SqlDbType.NVarChar).Value = dto.TenChuXe;
                cmd.Parameters.Add("@BienSo", SqlDbType.VarChar).Value = dto.BienSo;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = dto.DiaChi;
                cmd.Parameters.Add("@DienThoai", SqlDbType.VarChar).Value = dto.DienThoai;
                cmd.Parameters.Add("@HieuXe", SqlDbType.Int).Value = dto.HieuXe;
                cmd.Parameters.Add("@NgayTiepNhan", SqlDbType.DateTime).Value = dto.NgayTiepNhan;
                cmd.ExecuteNonQuery();
            }
            catch
            {
                return false;
            }
            finally
            {
                cn.Close();
            }
            return true;
        }
    }
}
